﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace YbxLibrary
{
    public class IniFileUtils
    {
        private string filePath;

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder value, int size, string path);

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        private static extern bool WritePrivateProfileString(string section, string key, string value, string path);

        public IniFileUtils(string path)
        {
            filePath = path;
        }

        public string ReadValue(string section, string key, string defaultValue = "")
        {
            StringBuilder sb = new StringBuilder(255);
            GetPrivateProfileString(section, key, defaultValue, sb, 255, filePath);
            return sb.ToString();
        }

        public void WriteValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, filePath);
        }

        /// <summary>
        /// 生成ini文件
        /// </summary>
        private static void GenerateIniFile()
        {
            string iniFilePath = "yb.ini";
            try
            {
                using (StreamWriter sw = new StreamWriter(iniFilePath))
                {
                    sw.WriteLine("[general]");
                    sw.WriteLine("fixmedins_code=H42030200820");
                    sw.WriteLine("fixmedins_name=十堰市太和医院");
                }
                Console.WriteLine("INI file '{iniFilePath}' generated successfully.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to generate INI file: {ex.Message}"+ex.Message);
            }
        }
    }
}
